import http from '@/http';

// 试卷提交API响应类型
export interface PaperSubmissionResponse {
  id: number;
  paper: {
    id: number;
    title: string;
  };
  user: string;
  start_time: string;
  submit_time: string;
  total_score: number;
  question_answers: {
    questions: {
      id: number;
      answer?: any;
      score?: number;
    }[];
    problems: {
      _id: string;
      submission_id?: string;
      code?: string;
      language?: string;
      score?: number;
    }[];
  };
}

// 提交请求体类型
export interface SubmitRequest {
  paper: number;
  question_answers: {
    questions: {
      id: number;
      answer: any;
    }[];
    problems: {
      _id: string;
      submission_id?: string;  // 如果有提交记录，使用submission_id
      code?: string;           // 如果没有提交记录，直接提交代码
      language?: string;       // 代码语言
    }[];
  };
}

// 试卷提交列表响应类型
export interface PaperSubmissionListResponse {
  count: number;
  results: PaperSubmissionResponse[];
}

enum url {
  paperSubmission = '/api/paper_submission/',
}

// 试卷提交服务
export const paperSubmissionService = {
  // 获取提交记录列表
  getSubmissions: (params?: { page?: number; page_size?: number; paper?: number }) => {
    return http.get<PaperSubmissionListResponse>(url.paperSubmission, { params });
  },

  // 创建提交记录
  submit: (data: SubmitRequest) => {
    return http.post<PaperSubmissionResponse>(url.paperSubmission, data);
  },

  // 获取单个提交记录
  getSubmission: (id: number) => {
    return http.get<PaperSubmissionResponse>(`${url.paperSubmission}${id}/`);
  },
}; 